#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""get information of dispatcher subboard info  采集分流平台子板信息
@author : buptzjj 
@license : Copyright@haohandata
@version : 1.0
@todo : 创建脚本  20171104  by buptzjj
"""

import sys
import os
import time

plugins_path = os.getenv("INSPECTOR_HOME") + "/plugins"
sys.path.append(plugins_path + "/common/")
import get_snmp_info
import get_ip_list
from dispatch_platform import *
import tools

ERR_FLAG = "No Such Object available on this agent at this OID"
TIME_OUT = 30
SECTION = "dispatcher"
KEY = "appadmin_password"
PASSWORD = tools.get_config_option(SECTION, KEY)


def main():
    div_ip_list = get_ip_list.get_ip_list("dispatch_platform")
    check_time = time.strftime("%s", time.localtime())
    for ip in div_ip_list:
        mfc = get_manufacturer(ip)
        if mfc == "恒为":
            div_obj = DivOptiway(ip, PASSWORD)
        elif mfc == "锐安":
            div_obj = DivRuian(ip, PASSWORD)
        elif mfc == "华3":
            div_obj = DivH3CComware(ip, PASSWORD)
        else:
            div_obj = DispatchPlatform(ip, PASSWORD)
        subboard_type_list = div_obj.get_subboard_type()
        subboard_status_list = div_obj.get_subboard_status()
        subboard_index = 1
        if ip.find(":") >= 0:
            ip = ip.split(":")[0]
        for subboard_type, subboard_status in zip(
            subboard_type_list, subboard_status_list
        ):
            print(
                "{0}\t{1}\t{2}\t{3}\t{4}\tdiv_subboard".format(
                    check_time, ip, subboard_index, subboard_type, subboard_status
                )
            )
            subboard_index += 1

        # 新增获取 上下行速度、接口当前运行状态
        if_status_list = div_obj.get_if_status()
        if_in_traffic_list = div_obj.get_if_in_traffic()
        if_out_traffic_list = div_obj.get_if_out_traffic()
        div_if_index = 1
        for if_status, if_in_traffic, if_out_traffic in zip(if_status_list, if_in_traffic_list, if_out_traffic_list):
            print("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\tdiv_subboard_if".format(
                check_time, ip, div_if_index, if_status, if_in_traffic, if_out_traffic)
            )
            div_if_index += 1


if __name__ == "__main__":
    main()
